var areasOpera = require("./model/KnowledgeArticle");
var express = require('express');
var app = express();
function getById(id, callback) {
    var updatestr = { "_id": id };
    areasOpera.KnowledgeArticle.findOne(updatestr, function (err, res) {
        if (err) {
            console.log("Error:" + err);
        }
        else {
            callback(res);
        }
    })
}

function getAllByPage(pageIndex, pageSize, callback) {
    var updatestr = {};
    areasOpera.KnowledgeArticle.find(updatestr).skip(pageIndex).limit(pageSize).exec(function (err, res) {
        if (err) {
            console.log("Error:" + err);
        }
        else {
            callback(res);
        }
    })
}

app.get('/aa', function (req, res) {
    getById(req.query.a, function (data) {
        res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" });
        res.end(JSON.stringify(data));
    });

})

app.get('/bb', function (req, res) {
    getAllByPage(req.query.i,req.query.s, function (data) {
        res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" });
        res.end(JSON.stringify(data));
    });

})

var server = app.listen(8081, function () {
    var host = server.address().address
    var port = server.address().port
    console.log("应用实例，访问地址为 http://%s:%s", host, port)
})
